package com.xiaoma.valid;

import com.xiaoma.annotation.NotBlank;
import com.xiaoma.annotation.NotNull;
import com.xiaoma.annotation.Regex;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @description  
 * @author wangtianxiang
 * @date: 2021/11/18 17:06
 */
public class ValidUtil {
    public static <T> List<String> valid(T o){
        List<String> list = new ArrayList<>();
        if(o == null){
            list.add("对象不能为空");
            return list;
        }
        list.addAll(notNullValid(o));
        list.addAll(notBlankValid(o));
        list.addAll(regexValid(o));
        return list;
    }



    private static <T> List<String> regexValid(T o){
        List<String> list = new ArrayList<>();
        Field[] fields  = o.getClass().getDeclaredFields();
        if(fields != null){
            for (Field field : fields) {

                Regex regex = field.getAnnotation(Regex.class);
                if(regex != null){
                    field.setAccessible(true);
                    try {
                        String value = (String)field.get(o);

                        if(StringUtils.isEmpty(value) || !Pattern.matches(regex.regex(),value)){
                            list.add(regex.message());
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return list;

    }


    private static <T> List<String> notBlankValid(T o){
        List<String> list = new ArrayList<>();
        Field[] fields  = o.getClass().getDeclaredFields();
        if(fields != null){
            for (Field field : fields) {

                NotBlank notBlank = field.getAnnotation(NotBlank.class);
                if(notBlank != null){
                    field.setAccessible(true);
                    try {
                        Object value = field.get(o);
                        if(StringUtils.isEmpty(value)){
                            list.add(notBlank.message());
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return list;

    }


    private static <T> List<String> notNullValid(T o){
        List<String> list = new ArrayList<>();
        Field[] fields  = o.getClass().getDeclaredFields();
        if(fields != null){
            for (Field field : fields) {

                NotNull notNull = field.getAnnotation(NotNull.class);
                if(notNull != null){
                    field.setAccessible(true);
                    try {
                        Object value = field.get(o);
                        if(value == null){
                            list.add(notNull.message());
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return list;
    }
}
